package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.InvoiceDTO;
import com.eastfair.pay.dto.InvoicePageQuery;
import com.eastfair.pay.dto.InvoiceSaveDTO;
import com.eastfair.pay.dto.InvoiceUpdateDTO;
import com.eastfair.pay.entity.Invoice;
import com.eastfair.pay.service.InvoiceService;
import com.eastfair.pay.vo.InvoiceVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 发票
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/invoice")
@Api(value = "Invoice", tags = "发票")
@PreAuth(replace = "pay:invoice:")
public class InvoiceController extends SuperController<InvoiceService, Long, Invoice, InvoicePageQuery, InvoiceSaveDTO, InvoiceUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<Invoice> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Invoice> invoiceList = list.stream().map((map) -> {
            Invoice invoice = Invoice.builder().build();
            //TODO 请在这里完成转换
            return invoice;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(invoiceList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<InvoicePageQuery> params) {
        InvoicePageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 确认开票
     */
    @PostMapping("/saveInvoicecCnfirm")
    public R saveInvoicecCnfirm(@RequestBody List<InvoiceDTO> invoiceDTO) {
        try {
            baseService.saveInvoicecCnfirm(invoiceDTO);
            return R.success();
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 开票记录查询
     */
    @PostMapping("/queryInvoicecList")
    public R<List<InvoiceVO>> queryInvoicecList(@RequestBody InvoiceDTO invoiceDTO) {
        try {
            List<InvoiceVO> invoiceVOList = baseService.queryInvoicecList(invoiceDTO);
            return  R.success(invoiceVOList);
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }
}
